package cn.edu.nankai.bookstore.controller.order;

import cn.edu.nankai.bookstore.domain.po.Customer;
import cn.edu.nankai.bookstore.domain.po.Order;
import cn.edu.nankai.bookstore.utils.Services;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CreateOrderServlet", value = "/CreateOrderServlet")
public class CreateOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type","text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("user");
        Order order = (Order) session.getAttribute("order");
        if(customer == null || order != null) return;
        int receiverId = Integer.parseInt(request.getParameter("receiver"));
        order = Services.orderService.create(customer.getCustomerId(), receiverId);
        session.setAttribute("order",order);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
